package cxf.spring.demo.gui;

import com.jgoodies.forms.builder.ButtonBarBuilder2;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.validation.ValidationResult;
import com.jgoodies.validation.ValidationResultModel;
import com.jgoodies.validation.util.DefaultValidationResultModel;
import com.jgoodies.validation.view.ValidationResultViewFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;

/**
 * User: Vlad Vinichenko (akerigan@gmail.com)
 * Date: 24.04.2009
 * Time: 14:36:37
 */
public abstract class EmptyDialog extends JDialog {

    ValidationResultModel validationResultModel = new DefaultValidationResultModel();
    JLabel messageLabel = ValidationResultViewFactory.createReportIconAndTextLabel(validationResultModel);
    boolean ok;

    protected abstract void initComponents();

    protected abstract JPanel getMainPanel();

    protected abstract ValidationResult validateData();

    public boolean isOk() {
        return ok;
    }

    public EmptyDialog(JFrame owner, String title, int width, int height) {
        super(owner, title);
        setDefaultCloseOperation(HIDE_ON_CLOSE);

        initComponents();

        FormLayout formLayout = new FormLayout(
                "3dlu, fill:pref:grow, 3dlu",
                "3dlu, fill:pref:grow, 3dlu, pref, 3dlu, pref");

        CellConstraints cellConstraints = new CellConstraints();

        getContentPane().setLayout(formLayout);
        getContentPane().add(getMainPanel(), cellConstraints.xy(2, 2));

        getContentPane().add(messageLabel, cellConstraints.xy(2, 4));

        ButtonBarBuilder2 buttonBarBuilder = new ButtonBarBuilder2();
        buttonBarBuilder.addGlue();

        JButton cancelButton = new JButton(cancelAction);
        JButton okButton = new JButton(okAction);
        getRootPane().setDefaultButton(okButton);

        buttonBarBuilder.addButton(okButton, cancelButton);

        getContentPane().add(buttonBarBuilder.getPanel(), cellConstraints.xy(2, 6));

        setSize(width, height);
        setVisible(true);
    }

    protected Action okAction = new Action() {
        public Object getValue(String key) {
            if (Action.NAME.equals(key)) {
                return "ОК";
            } else if (Action.SELECTED_KEY.equals(key)) {
                return true;
            } else if (Action.DEFAULT.equals(key)) {
                return true;
            } else {
                return null;
            }
        }

        public void putValue(String key, Object value) {
            // do nothing
        }

        public void setEnabled(boolean b) {
            // do nothing
        }

        public boolean isEnabled() {
            return true;
        }

        public void addPropertyChangeListener(PropertyChangeListener listener) {
            // do nothing
        }

        public void removePropertyChangeListener(PropertyChangeListener listener) {
            // do nothing
        }

        public void actionPerformed(ActionEvent e) {
            ValidationResult validationResult = validateData();
            if (validationResult != null) {
                validationResultModel.setResult(validationResult);
                ok = !validationResult.isEmpty();
            } else {
                ok = true;
            }
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    setVisible(ok);
                }
            });
        }
    };

    protected Action cancelAction = new Action() {

        public Object getValue(String key) {
            if (Action.NAME.equals(key)) {
                return "Отмена";
            } else if (Action.SELECTED_KEY.equals(key)) {
                return false;
            } else if (Action.DEFAULT.equals(key)) {
                return false;
            } else {
                return null;
            }
        }

        public void putValue(String key, Object value) {
            // do nothing
        }

        public void setEnabled(boolean b) {
            // do nothing
        }

        public boolean isEnabled() {
            return true;
        }

        public void addPropertyChangeListener(PropertyChangeListener listener) {
            // do nothing
        }

        public void removePropertyChangeListener(PropertyChangeListener listener) {
            // do nothing
        }

        public void actionPerformed(ActionEvent e) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    setVisible(false);
                }
            });
        }
    };
}